package KK.Controls.Tips
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class TBox extends Bitmap implements IBox
	{
		
		static private const NONE:int=0;//空
		static private const SKIN:int=1;//有皮肤
		
		static private var AUTO_INCREMENT:int=0;
		static private function get AI():int{
			return AUTO_INCREMENT++;
		}
		////////////////////////////////////////////
		static private const B_LT:int=AI;//左上
		static private const B_MT:int=AI;//中上
		static private const B_RT:int=AI;//右上
		static private const B_ML:int=AI;//中间左边
		static private const B_MM:int=AI;//正中
		static private const B_MR:int=AI;//中右
		static private const B_BL:int=AI;//下左
		static private const B_BM:int=AI;//下中
		static private const B_BR:int=AI;//下右
		
		static private const B_MAX:int=AI;// 长度
		
		private var box:Vector.<BitmapData>;
		///////////////////////////////////////////
		
		
		private var status:int;
		
		
		private var size:Point;//大小
		private var nLump:Point;//块大小
		public function TBox()
		{
			init();
		}
		
		private function init ():void{ 
			size=new Point();
			nLump=new Point();
		}
		
		/**
		 * 重绘 
		 * 
		 */ 
		public function ReDraw():void{
			
			if(!box){
				return ;
			}
			// 转换大小表
			var W_Lump:int=Lump.x;
			var H_Lump:int=Lump.y; 
			var pointTable:Vector.<Point>=new Vector.<Point>(B_MAX,true);
			 
			
			var width:int=W_Lump*2;
			var height:int=H_Lump*2;
			
			width=this.width>width?this.width:width;
			height=this.height>height?this.height:height;
			
			const iLen:int=3,jLen:int=3;
			var i:int=0,j:int=0;
			
			while(i<B_MAX){
				pointTable[i]=new Point();
				i++;
			}
			
			pointTable[B_LT].x=pointTable[B_ML].x=pointTable[B_MR].x=pointTable[B_RT].x=pointTable[B_BL].x=pointTable[B_BR].x=W_Lump;
			pointTable[B_BM].y=pointTable[B_MT].y=pointTable[B_LT].y=pointTable[B_RT].y=pointTable[B_BL].y=pointTable[B_BR].y=H_Lump;
			//十字坐标
			pointTable[B_MT].x=pointTable[B_MM].x=pointTable[B_BM].x=width-W_Lump*2;
			pointTable[B_ML].y=pointTable[B_MM].y=pointTable[B_MR].y=height-H_Lump*2;
			
			
			var bd:BitmapData=new BitmapData(width,height,true,0x0);
			
			
			
			var point:Point=new Point();
			var rect:Rectangle=new Rectangle();
			
			var size:Point=new Point();
			
			var index:int=0;
			for(i=0;i<iLen;i++){
				index=i*jLen;
				for(j=0;j<jLen;j++){
					rect.width=pointTable[index+j].x;
					rect.height=pointTable[index+j].y;
					size.x=rect.width;
					size.y=rect.height;
					if(!(size.x==0||size.y==0)){
						bd.copyPixels(FillBitmapData(box[index+j],size),rect,point);
					} 
					point.x+=pointTable[index+j].x;
				}
				point.y+=pointTable[index+j-1].y;//增加高度
				point.x=0;
			}
			
			this.bitmapData=bd; 
		} 
		/**
		 * 设置数据 
		 * @param bd
		 * @return 
		 * 
		 */
		public function SetBitmapData(bm:BitmapData,W_Lump:int,H_Lump:int):Vector.<BitmapData>{
			
			var result:Vector.<BitmapData>=new Vector.<BitmapData>(B_MAX,true);
			
			var point:Point=new Point(0,0);
			var rect:Rectangle=new Rectangle(0,0,0,0);
			
			const WIDTH:int=W_Lump;
			const HEIGHT:int=H_Lump;
			
			nLump.x=WIDTH;
			nLump.y=HEIGHT;
			
			
			//左上角
			var bd:BitmapData=new BitmapData(W_Lump,H_Lump);
			rect.x=0;
			rect.y=0;
			rect.width=WIDTH;
			rect.height=HEIGHT;
			bd.copyPixels(bm,rect,point);
			result[B_LT]=bd;
			
			//上
			bd=new BitmapData(W_Lump,H_Lump);
			rect.x=WIDTH;
			rect.y=0;
			bd.copyPixels(bm,rect,point); 
			result[B_MT]=bd;
			
			//右上角
			bd=new BitmapData(W_Lump,H_Lump);
			rect.x=bm.width-WIDTH;
			rect.y=0;
			bd.copyPixels(bm,rect,point);
			result[B_RT]=bd;
			
			//中左角
			bd=new BitmapData(W_Lump,H_Lump);
			rect.x=0;
			rect.y=HEIGHT;
			bd.copyPixels(bm,rect,point);
			result[B_ML]=bd;
			
			//中中角
			bd=new BitmapData(W_Lump,H_Lump);
			rect.x=WIDTH;
			rect.y=HEIGHT;
			bd.copyPixels(bm,rect,point);
			result[B_MM]=bd;
			
			//中右角
			bd=new BitmapData(W_Lump,H_Lump);
			rect.x=bm.width-WIDTH;
			rect.y=HEIGHT;
			bd.copyPixels(bm,rect,point);
			result[B_MR]=bd;
			
			//下左角
			bd=new BitmapData(W_Lump,H_Lump);
			rect.x=0;
			rect.y=bm.height-HEIGHT;
			bd.copyPixels(bm,rect,point);
			result[B_BL]=bd;
			
			//下中角
			bd=new BitmapData(W_Lump,H_Lump);
			rect.x=WIDTH;
			rect.y=bm.height-HEIGHT;
			bd.copyPixels(bm,rect,point);
			result[B_BM]=bd;
			
			//下右角
			bd=new BitmapData(W_Lump,H_Lump);
			rect.x=bm.width-WIDTH;
			rect.y=bm.height-HEIGHT;
			bd.copyPixels(bm,rect,point);
			result[B_BR]=bd;
			
			
			box=result; 
			ReDraw();
			State=SKIN;
			return result;
		}
		
		
		/**
		 * 填充成一张制定大小的bitmap 
		 * @param bd 源图像
		 * @param size 最终大小
		 * @return 填充图像
		 * 
		 */
		public function  FillBitmapData(sourceBitmap:BitmapData,size:Point):BitmapData{
			if(sourceBitmap.width<size.x||size.y>sourceBitmap.height){
				var result:BitmapData=new BitmapData(size.x,size.y,true,0);
				var point:Point=new Point();
				var sourceRect:Rectangle=new Rectangle(); 
				
				sourceRect.y=sourceRect.x=0;
				sourceRect.width=sourceBitmap.width;
				sourceRect.height=sourceBitmap.height;
				while(point.y<size.y){
					sourceRect.height=(size.y-point.y>sourceBitmap.height?sourceBitmap.height:point.y-(point.y-sourceBitmap.height));
					while(point.x<size.x){
						sourceRect.width=(size.x-point.x>sourceBitmap.width?sourceBitmap.width:point.x-(point.x-sourceBitmap.width));
						result.copyPixels(sourceBitmap,sourceRect,point);
						point.x+=sourceBitmap.height;
					}
					point.y+=sourceBitmap.width;
					point.x=0;
				}
			}else{
				result=sourceBitmap;
			}
			return result;
		}
		
		override public function set width(value:Number):void{ 
			if(State==SKIN&&box[B_LT].width*2>value){
				value=box[B_LT].width*2;
			}
//			super.width=value;
			size.x=value;
//			ReDraw();
		}
		override public function set height(value:Number):void{
			if(State==SKIN&&box[B_LT].height*2>value){
				value=box[B_LT].height*2;
			}
//			super.height=value;
			size.y=value;
//			ReDraw();
		}
		
		override public function get width():Number{
			return size.x;
		}
		override public function get height():Number{
			return size.y;
		}
		
		/**
		 * 状态 
		 * @return 
		 * 
		 */
		private function get State():int{
			return this.status
		}
		private function set State(value:int):void{
			this.status=value;
		}
		
		/**
		 * 设置大小 
		 * @param width
		 * @param height
		 * 
		 */
		public function SetSize(width:int,height:int):void{
			size.x=width;
			size.y=height;
			ReDraw();
		}
		
		
		/**
		 * 返回块大小 
		 * @return 
		 * 
		 */
		public function get Lump():Point{
			var result:Point;
			result=nLump;
			return result;
		}
	}
}